package com.example.dicomviewer.core;

import android.util.Log;

public class DataArray {
    private static final String TAG = "DataArray";
    private int[][][] pixel;
    private byte[] fixedPixel;
    private int sliceSize = 0, width = 0, hight = 0;
    private int boneValue = 145;
    private int visceraValue = 90;

    public int getSliceSize() {
        return sliceSize;
    }

    public void setSliceSize(int sliceSize) {
        this.sliceSize = sliceSize;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHight() {
        return hight;
    }

    public void setHight(int hight) {
        this.hight = hight;
    }

    private static DataArray instance = null;

    public byte[] getFixedPixel() {
        return fixedPixel;
    }

    public int[][][] getPixel() {
        return pixel;
    }

    public void setPixel(int[][][] pixel) {
        this.pixel = pixel;
        // todo: 之后进行更改
        createFixedPixel();
    }

    private void createFixedPixel() {
        Log.d(TAG, "begin createFixedPixel");
        int slice = getSliceSize();
        int width = getWidth();
        int hight = getHight();
        // rgba
        this.fixedPixel = new byte[slice * width * hight * 4];
        reset();
    }

    public void reset() {
        int slice = getSliceSize();
        int width = getWidth();
        int hight = getHight();
        int cnt = 0;
        int ans;
        for (int i = 0; i < slice; ++i) {
            for (int j = 0; j < width; ++j) {
                for (int k = 0; k < hight; ++k) {
                    if (this.pixel[i][j][k] == -16777216) {
                        this.fixedPixel[cnt] = (byte) 0;
                        cnt++;
                        this.fixedPixel[cnt] = (byte) 0;
                        cnt++;
                        this.fixedPixel[cnt] = (byte) 0;
                        cnt++;
                        this.fixedPixel[cnt] = (byte) 0;
                        cnt++;
                        continue;
                    }
                    ans = (this.pixel[i][j][k] + 16777216) / 65793;
                    if (ans >= 145) { // 骨骼
                        this.fixedPixel[cnt] = (byte) 255;
                        cnt++;
                        this.fixedPixel[cnt] = (byte) 255;
                        cnt++;
                        this.fixedPixel[cnt] = (byte) 255;
                        cnt++;
                        this.fixedPixel[cnt] = (byte) 254;
                        cnt++;
                    } else if (ans >= 90) { // 内脏
                        this.fixedPixel[cnt] = (byte) 110;
                        cnt++;
                        this.fixedPixel[cnt] = (byte) 46;
                        cnt++;
                        this.fixedPixel[cnt] = (byte) 29;
                        cnt++;
                        this.fixedPixel[cnt] = (byte) 30;
                        cnt++;
                    } else { // 皮肤
                        this.fixedPixel[cnt] = (byte) 173;
                        cnt++;
                        this.fixedPixel[cnt] = (byte) 110;
                        cnt++;
                        this.fixedPixel[cnt] = (byte) 54;
                        cnt++;
                        this.fixedPixel[cnt] = (byte) 0;
                        cnt++;
                    }
                }
            }
        }
    }

    public static int BONE = 1;
    public static int VISCERA = 2;
    public static int VISABLE = 0;
    public static int DISVISABLE = 255;
    private int visible[] = new int[]{0, 0, 0, 0};

    public void changeVisibleByOrgan(int organCode) {
        if (visible[organCode] == 1) {
            visible[organCode] = 0;
            setVisible(organCode, DISVISABLE);
        } else {
            visible[organCode] = 1;
            setVisible(organCode, VISABLE);
        }
    }

    private void setVisible(int enableCode, int visableCode) {
        int slice = getSliceSize();
        int width = getWidth();
        int hight = getHight();
        int cnt = 0;
        for (int i = 0; i < slice; ++i) {
            for (int j = 0; j < width; ++j) {
                for (int k = 0; k < hight; ++k) {
                    int ans = (this.pixel[i][j][k] + 16777216) / 65793;
                    if (enableCode == BONE) {
                        if (ans >= boneValue) {
                            this.fixedPixel[cnt + 3] = (byte) visableCode;
                        }
                    } else if (enableCode == VISCERA) {
                        if (boneValue > ans && ans >= visceraValue) {
                            this.fixedPixel[cnt + 3] = (byte) visableCode;
                        }
                    } else {
                        if (visceraValue > ans) {
                            this.fixedPixel[cnt + 3] = (byte) visableCode;
                        }
                    }
                    cnt += 4;
                }
            }
        }
    }

    // 单例模式： 双检锁/双重校验锁
    public static DataArray getInstance() {
        if (instance == null) {
            synchronized (DataArray.class) {
                if (instance == null) {
                    instance = new DataArray();
                }
            }
        }
        return instance;
    }
}
